//
// Created by 段大志 on 16/4/23.
// Copyright (c) 2016 BMProject. All rights reserved.
//

#import "BackgroundDealVc.h"
#import "DataModel.h"
#import "BaseTableView.h"
#import "UIViewController+common.h"
#import "UIColor+Extension.h"
#import "BackgroundBizDealCell.h"
#import "BackgroundBizDisCell.h"


@interface BackgroundDealVc ()  <UITableViewDelegate, UITableViewDataSource> {

}
@property(strong, nonatomic)  BaseTableView  *tableView;
@property(strong, nonatomic)  BlackListModel *blackListModel;
@property(strong, nonatomic)  BackgroundBizDealCell *backgroundBizDealCell;
@property(strong, nonatomic)  BackViewWrapModel *backViewWrapModel;
@end

@implementation BackgroundDealVc {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog(@"--后台业务处理--");
    NAV_INIT(self, @"后台业务处理", @"back", @selector(backButtonClick:), @"", nil);
    [self initTab];

    self.blackListModel = self.blackListWrapModel.blackListModels[self.position];
    [self request_deal];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}


- (void)initTab {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(
            0,
            0,
            UIScreenWidth,
            UIScreenHeight - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor backgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableFooterView.backgroundColor = [UIColor backgroundColor];
    [self.tableView reloadData];
}


#pragma mark  ------- UITableView UITableViewDelegate or UITableViewDataSource---------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self getCount];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [self getCount]-1) {
        return 240;
    }
    return 135 + 10;
}

- (int) getCount {
     return self.backViewWrapModel.backMessList.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [self getCount]-1) {
        BackgroundBizDealCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BackgroundBizDealCell"];
        if (!cell)
            cell = [[[UINib nibWithNibName:@"BackgroundBizDealCell" bundle:nil] instantiateWithOwner:self options:nil] lastObject];
        [cell.confrimBtn addTarget:self action:@selector(confrimAction:) forControlEvents:UIControlEventTouchUpInside];
        self.backgroundBizDealCell = cell;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        self.blackListModel = self.blackListWrapModel.blackListModels[self.position];
        cell.contentTv.text = [NSString stringWithFormat:@"Re:%@", [NSString ignoreEmptyStr:self.blackListModel.title]];
        return cell;
    } else {
        BackgroundBizDisCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BackgroundBizDisCell"];
        if (!cell)
            cell = [[[UINib nibWithNibName:@"BackgroundBizDisCell" bundle:nil] instantiateWithOwner:self options:nil] lastObject];
        cell.backgroundDealVc = self;
        [cell setCellInfo:self.backViewWrapModel andPosition:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)confrimAction:(UIButton *)sender {
    if (self.backgroundBizDealCell.contentTv.text.trimString.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"标题不能为空"];
        return;
    }
    if (self.backgroundBizDealCell.titleView.text.trimString.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"内容不能为空"];
        return;
    }
    [self request_deal_replay];
}

#pragma mark  -- 网络处理 --

- (void)request_deal {
    [UserServices blackDealToken:[[AppDelegate appDelegate] getUserInfo].token
                              id:self.blackListModel.id
                          backId:self.blackListModel.gdId
                      showDialog:YES completionBlock:^(int result, id responseObject) {
                if (result == API_Request_Success) {
                    NSMutableArray *array = responseObject;
                    [self.tableView.footer resetNoMoreData];//重置有更多数据
                    if (array == nil || array.count == 0) {
                        //提示无数据
                        [SVProgressHUD showErrorWithStatus:@"查无数据"];
                    } else {
                        _backViewWrapModel = [[BackViewWrapModel alloc] initWithDictionary:array[0] error:nil];
                        NSMutableArray<BackMessListModel*> *list = [NSMutableArray<BackMessListModel*> array];
                        [list addObjectsFromArray:_backViewWrapModel.backMessList];
                        if (_backViewWrapModel.blackMess) {
                            [list addObject:_backViewWrapModel.blackMess];
                        }
                        _backViewWrapModel.backMessList = list;
                    }
                    [self.tableView.footer resetNoMoreData];//重置有更多数据
                    [self.tableView.header endRefreshing];
                    [self.tableView.footer endRefreshing];
                    [self.tableView reloadData];
                    [self.tableView.footer noticeNoMoreData];//没有更多数据
                } else {//出错时的处理
                    //提示出错
                    [SVProgressHUD showErrorWithStatus:Error_Format];
                }
            }];
}

- (void) request_deal_replay {
    [UserServices blackDealToken:[[AppDelegate appDelegate] getUserInfo].token
                              id:self.blackListModel.id
                          backId:self.blackListModel.gdId
                       recUserId:self.blackListModel.recUserId
                         reTitle:self.backgroundBizDealCell.contentTv.text
                         message:self.backgroundBizDealCell.titleView.text
                      showDialog:YES
                 completionBlock:^(int result, id responseObject) {
                     if (result == API_Request_Success) {
                        DLog(@"回复成功.......")
                         if (self.block) {
                             self.block(YES);
                         }
                         [self.navigationController popViewControllerAnimated:YES];
                     } else {//出错时的处理
                         //提示出错
                         [SVProgressHUD showErrorWithStatus:Error_Format];
                     }
                 }];
}

@end
